// 调用dll返回状态码
const hctpRetCode = {
  HCTP_RET_OK: 0,
  HCTP_RET_ERROR_MALLOC: 1,
  HCTP_RET_ERROR_PARAM: 2,
  HCTP_RET_ERROR_TRANS_MANUAL_STOP: 3,
  HCTP_RET_ERROR_TRANS_TASK_DUPLICATED: 4,
  HCTP_RET_ERROR_TRANS_TASK_LIST_FULL: 5,
  HCTP_RET_ERROR_TUNNEL_INIT: 6,
  HCTP_RET_ERROR_PPCS_CONNECT: 7,
  HCTP_RET_ERROR_PPCS_BROKEN: 8,
  HCTP_RET_ERROR_PPCS_WRITE: 9,
  HCTP_RET_ERROR_PPCS_CLOSE: 10,
  HCTP_RET_ERROR_RECVREPLY_TIMEOUT: 11,
  HCTP_RET_ERROR_LOCAL_MD5_CALC: 12,
  HCTP_RET_ERROR_FILE_OPEN: 13,
  HCTP_RET_ERROR_FILE_SIZE: 14,
  HCTP_RET_ERROR_FILE_READ: 15,
  HCTP_RET_ERROR_FILE_WRITE: 16,
  HCTP_RET_ERROR_WRONG_OFFSET: 17,
  HCTP_RET_ERROR_MAGIC_HDR: 18,
  HCTP_RET_ERROR_MAGIC_HDR_OR_MALLOC: 19,
  HCTP_RET_ERROR_DATATRANS_MD5: 20,
  HCTP_RET_ERROR_TRANS_REJECTED: 21,
  HCTP_RET_ERROR_TRANS_ABORT_FAILED: 22,
  HCTP_RET_ERROR_QUEUE_NOTFOUND: 23,
  NO_SUCH_FILE_OR_DIRECTORY: 10000,
  ERROR_FULLWIDTH_FILENAME: 10004, // 文件名含全角字符
  ERROR_WORKER_OTHER: 10005, // 其他错误
  ERROR_CHILD_PROCESS_EXIT: 10006, // 进程退出
  ERROR_TO_GET_AUTHENTICATION: 10007, // 获取认证失败
  ERROR_FILE_NAME_LENGTH_EXCEEDS_255: 10008, // 文件名称长度超过255个字节
  NO_SUCH_DOWNLOAD_FILE_OR_DIRECTORY: 10009 // 下载目录不存在
};

const mapHctpRetCode = new Map([
  [hctpRetCode.HCTP_RET_OK, {text: '一切正常'}],
  [hctpRetCode.HCTP_RET_ERROR_MALLOC, {text: '分配内存出错'}],
  [hctpRetCode.HCTP_RET_ERROR_PARAM, {text: '参数错误'}],
  [hctpRetCode.HCTP_RET_ERROR_TRANS_MANUAL_STOP, {text: '手动停止'}],
  [hctpRetCode.HCTP_RET_ERROR_TRANS_TASK_DUPLICATED, {text: '任务重复'}],
  [hctpRetCode.HCTP_RET_ERROR_TRANS_TASK_LIST_FULL, {text: '内部列表满'}],
  [hctpRetCode.HCTP_RET_ERROR_TUNNEL_INIT, {text: '内部隧道初始化出错'}],
  [hctpRetCode.HCTP_RET_ERROR_PPCS_CONNECT, {text: '连接出错'}],
  [hctpRetCode.HCTP_RET_ERROR_PPCS_BROKEN, {text: '内部断开'}],
  [hctpRetCode.HCTP_RET_ERROR_PPCS_WRITE, {text: '写错误'}],
  [hctpRetCode.HCTP_RET_ERROR_PPCS_CLOSE, {text: '关闭错误'}],
  [hctpRetCode.HCTP_RET_ERROR_RECVREPLY_TIMEOUT, {text: '接收回应超时'}],
  [hctpRetCode.HCTP_RET_ERROR_LOCAL_MD5_CALC, {text: '本地MD5计算出错'}],
  [hctpRetCode.HCTP_RET_ERROR_FILE_OPEN, {text: '打开文件错误'}],
  [hctpRetCode.HCTP_RET_ERROR_FILE_SIZE, {text: '文件大小异常'}],
  [hctpRetCode.HCTP_RET_ERROR_FILE_READ, {text: '文件读错误'}],
  [hctpRetCode.HCTP_RET_ERROR_FILE_WRITE, {text: '本地文件写错误'}],
  [hctpRetCode.HCTP_RET_ERROR_WRONG_OFFSET, {text: '文件位移错误'}],
  [hctpRetCode.HCTP_RET_ERROR_MAGIC_HDR, {text: '魔法头错误'}],
  [hctpRetCode.HCTP_RET_ERROR_MAGIC_HDR_OR_MALLOC, {text: '魔法或分配错误'}],
  [hctpRetCode.HCTP_RET_ERROR_DATATRANS_MD5, {text: '传输MD5错误'}],
  [hctpRetCode.HCTP_RET_ERROR_TRANS_REJECTED, {text: '传输拒绝'}],
  [hctpRetCode.HCTP_RET_ERROR_TRANS_ABORT_FAILED, {text: 'Abort错误'}],
  [hctpRetCode.HCTP_RET_ERROR_QUEUE_NOTFOUND, {text: '最大错误'}],
  [hctpRetCode.NO_SUCH_FILE_OR_DIRECTORY, {text: '文件已被移除'}],
  [hctpRetCode.ERROR_FULLWIDTH_FILENAME, {text: '文件名含全角字符'}],
  [hctpRetCode.ERROR_WORKER_OTHER, {text: '其他错误'}],
  [hctpRetCode.ERROR_CHILD_PROCESS_EXIT, {text: '进程退出'}],
  [hctpRetCode.ERROR_TO_GET_AUTHENTICATION, {text: '获取认证失败'}],
  [hctpRetCode.ERROR_FILE_NAME_LENGTH_EXCEEDS_255, {text: '文件名称长度超过255个字节'}],
  [hctpRetCode.NO_SUCH_DOWNLOAD_FILE_OR_DIRECTORY, {text: '下载目录不存在'}]
]);

const HIFI_CODES = {
  TRANSFER_OK: 0x0,
  TRANSFER_DUPLICATION: 0x01,
  TRANSFER_FOLDER_NOT_EXIST: 0x02,
  TRANSFER_PARAMETER_ERROR: 0x03,
  TRANSFER_MD5_MISMATCH: 0x04,
  TRANSFER_SAME_REACH_MAX: 0x05,
  TRANSFER_SAME_FILE: 0x06,
  TRANSFER_DISK_FULL: 0x07,
  TRANSFER_DISK_LOST: 0x08,
  TRANSFER_SAME_TASK: 0x09,
  TRANSFER_FILE_NOT_EXIST: 0x10,
  TRANSFER_NOT_READY: 0x12,
  TRANSFER_OWNER_MISMATCH: 0x13,
  TRANSFER_DEVICE_WRITE_ERROR: 0x14,
  TRANSFER_DEVICE_READ_ERROR: 0x15,
  TRANSFER_SAME_FOLDER: 0x16,
  TRANSFER_CREATE_FOLDER_FAIL: 0x17,
  TRANSFER_CREATE_FOLDER_DUPLICATION: 0x18,
  TRANSFER_SAME_MD5: 0x20,
  TRANSFER_DEVICE_EXCEPTION1: 0x40,
  TRANSFER_DEVICE_EXCEPTION2: 0x41,
  TRANSFER_NO_CHANNEL: 0x42,
  TRANSFER_QUEUE_SUSPEND: 0x43,
  TRANSFER_EXCEPTION_EXCEPTION3: 0x44,
  TRANSFER_COVER_SAME_FILE: 0x45,
  TRANSFER_TOKEN_EXPIRE: 0x50,
  TRANSFER_AUTH_DENIED: 0x51,
  TRANSFER_CVS_EXCEED_RECORD: 0x60
};

function getReturnCode(ret) {
  const hifiCode = (ret >> 16) & 0xffff;
  const dllCode = ret & 0xffff;

  let text = '';

  switch (hifiCode) {
    case HIFI_CODES.TRANSFER_OK:
      text = '正常传输';
      break;
    case HIFI_CODES.TRANSFER_DUPLICATION:
      text = '文件重复';
      break;
    case HIFI_CODES.TRANSFER_FOLDER_NOT_EXIST:
      text = '上传目录不存在';
      break;
    case HIFI_CODES.TRANSFER_PARAMETER_ERROR:
      text = '协议参数不正确';
      break;
    case HIFI_CODES.TRANSFER_MD5_MISMATCH:
      text = 'MD5不匹配';
      break;
    case HIFI_CODES.TRANSFER_SAME_REACH_MAX:
      text = '重复文件已经有16个';
      break;
    case HIFI_CODES.TRANSFER_SAME_FILE:
      text = '检查到有重复文件，但APP不是cover状态';
      break;
    case HIFI_CODES.TRANSFER_DISK_FULL:
      text = '硬盘满';
      break;
    case HIFI_CODES.TRANSFER_DISK_LOST:
      text = '硬盘挂载点丢失';
      break;
    case HIFI_CODES.TRANSFER_SAME_TASK:
      text = '上一任务未结束，重复上传name+md5 一致的文件';
      break;
    case HIFI_CODES.TRANSFER_FILE_NOT_EXIST:
      text = '文件不存在';
      break;
    case HIFI_CODES.TRANSFER_NOT_READY:
      text = '设备未准备好，不能传输';
      break;
    case HIFI_CODES.TRANSFER_OWNER_MISMATCH:
      text = 'OWNER 不一致';
      break;
    case HIFI_CODES.TRANSFER_DEVICE_WRITE_ERROR:
      text = '设备端写错误';
      break;
    case HIFI_CODES.TRANSFER_DEVICE_READ_ERROR:
      text = '上传, 设备端10s内没有读到任何信息';
      break;
    case HIFI_CODES.TRANSFER_SAME_FOLDER:
      text = '存在同名目录';
      break;
    case HIFI_CODES.TRANSFER_CREATE_FOLDER_FAIL:
      text = '资源管理器创建目录失败';
      break;
    case HIFI_CODES.TRANSFER_CREATE_FOLDER_DUPLICATION:
      text = '上传文件夹，相同目录已经存在相同的文件夹';
      break;
    case HIFI_CODES.TRANSFER_SAME_MD5:
      text = '已经有了相同的MD5Blob';
      break;
    case HIFI_CODES.TRANSFER_DEVICE_EXCEPTION1:
      text = '设备端异常1, 设备端没找到对应任务列表';
      break;
    case HIFI_CODES.TRANSFER_DEVICE_EXCEPTION2:
      text = '设备端异常2, 没有解出传输协议中的body';
      break;
    case HIFI_CODES.TRANSFER_NO_CHANNEL:
      text = '没有多余的读写通道';
      break;
    case HIFI_CODES.TRANSFER_QUEUE_SUSPEND:
      text = '设备端QUEUE阻塞';
      break;
    case HIFI_CODES.TRANSFER_EXCEPTION_EXCEPTION3:
      text = '设备端异常3, 入参不对';
      break;
    case HIFI_CODES.TRANSFER_COVER_SAME_FILE:
      text = 'cover模式上传文件，目的文件夹下存在同名文件夹';
      break;
    case HIFI_CODES.TRANSFER_TOKEN_EXPIRE:
      text = 'Token过期';
      break;
    case HIFI_CODES.TRANSFER_AUTH_DENIED:
      text = '无权限';
      break;
    case HIFI_CODES.TRANSFER_CVS_EXCEED_RECORD:
      text = '资源管理器版本超上限 （256个版本记录)';
      break;
    default:
      text = `未知的云存宝返回码: ${hifiCode}`;
  }

  return {
    ret,
    text,
    hifiCode,
    dllCode
  };
}
const getMapHctpRetText = (code) => {
  return (mapHctpRetCode.get(code) || getReturnCode(code) || {text: ''}).text;
};
module.exports = {HIFI_CODES, getReturnCode, hctpRetCode, mapHctpRetCode, getMapHctpRetText};
